package com.candykingdom.components
{
	import com.candykingdom.base.Component;
	import com.candykingdom.events.ControlEvent;
	
	public class Button extends Component
	{
		public function Button()
		{
			super();
			this.enabledDrag = true;
			this.addEventListener(ControlEvent.DOWN ,controlHandler);
		}
		
		private function controlHandler(event:ControlEvent):void
		{
			switch(event.type)
			{
				case ControlEvent.DOWN:
					this.addEventListener(ControlEvent.UP ,controlHandler);
					this.addEventListener(ControlEvent.OUT ,controlHandler);
					this.addEventListener(ControlEvent.DRAG_START ,controlHandler);
					gotoAndStop(2);
					return;
					
				case ControlEvent.UP:case ControlEvent.OUT:case ControlEvent.DRAG_START:
					event.stopPropagation();
					hide();
					return;
			}
		}
		
		private function hide():void
		{
			this.removeEventListener(ControlEvent.UP ,controlHandler);
			this.removeEventListener(ControlEvent.OUT ,controlHandler);
			this.removeEventListener(ControlEvent.DRAG_START ,controlHandler);
			gotoAndStop(1);
		}
	}
}